package com.redijedi.tapestry.components;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.annotations.AfterRenderTemplate;
import org.apache.tapestry.annotations.BeforeRenderTemplate;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.annotations.SetupRender;
import org.apache.tapestry.annotations.SupportsInformalParameters;

/**
 * Helper component that will render a variable element type or nothing if null
 * is passed in. Similar to the deprecated Any component.
 * 
 * @author torr
 * 
 */
@SupportsInformalParameters
public class Element {

	@Inject
	private ComponentResources _resources;

	@Parameter(defaultPrefix = "literal")
	private String _elementName;

	@SetupRender
	void setup() {
		if (_elementName == "null") {
			_elementName = null;
		}
	}

	@BeforeRenderTemplate
	void before(MarkupWriter writer) {
		if (_elementName != null) {
			writer.element(_elementName);
			_resources.renderInformalParameters(writer);
		}
	}

	@AfterRenderTemplate
	void after(MarkupWriter writer) {
		if (_elementName != null) {
			writer.end();
		}
	}
}
